﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;

namespace Himp.Erp.EntityFrameworkCore;

[DependsOn(
    typeof(ErpDomainModule),
    typeof(AbpEntityFrameworkCoreModule)
)]
public class ErpEntityFrameworkCoreModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<ErpDbContext>(options =>
        {
            options.AddDefaultRepositories(includeAllEntities: true);

            options.Entity<DrugCatalogExt>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.DrugPrdr); };
            });

            options.Entity<DrugCatalog>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.ChrgitmType); };
            });

            options.Entity<InboundDetl>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.MatlInfo); };
            });

            options.Entity<InboundRec>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.MatlSpler).Include(p => p.InboundDetls).ThenInclude(pp=>pp.MatlInfo); };
            });

            options.Entity<MatlInv>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.MatlInfo).Include(p => p.MatlWhse); };
            });

            options.Entity<OutboundRec>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.MatlWhse).Include(pp => pp.OutboundDetls).ThenInclude(pp=>pp.MatlInfo); };
            });
        });
    }
}
